<html>
<head>
<title>TokenServiceImpl.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #cf8e6d;}
.s1 { color: #bcbec4;}
.s2 { color: #bcbec4;}
.s3 { color: #5f826b; font-style: italic;}
.s4 { color: #6aab73;}
.s5 { color: #2aacb8;}
</style>
</head>
<body bgcolor="#1e1f22">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
TokenServiceImpl.java</font>
</center></td></tr></table>
<pre><span class="s0">import </span><span class="s1">com</span><span class="s2">.</span><span class="s1">baomidou</span><span class="s2">.</span><span class="s1">mybatisplus</span><span class="s2">.</span><span class="s1">mapper</span><span class="s2">.</span><span class="s1">EntityWrapper</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">com</span><span class="s2">.</span><span class="s1">baomidou</span><span class="s2">.</span><span class="s1">mybatisplus</span><span class="s2">.</span><span class="s1">mapper</span><span class="s2">.</span><span class="s1">Wrapper</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">com</span><span class="s2">.</span><span class="s1">baomidou</span><span class="s2">.</span><span class="s1">mybatisplus</span><span class="s2">.</span><span class="s1">plugins</span><span class="s2">.</span><span class="s1">Page</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">com</span><span class="s2">.</span><span class="s1">baomidou</span><span class="s2">.</span><span class="s1">mybatisplus</span><span class="s2">.</span><span class="s1">service</span><span class="s2">.</span><span class="s1">impl</span><span class="s2">.</span><span class="s1">ServiceImpl</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">com</span><span class="s2">.</span><span class="s1">dao</span><span class="s2">.</span><span class="s1">TokenDao</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">com</span><span class="s2">.</span><span class="s1">entity</span><span class="s2">.</span><span class="s1">TokenEntity</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">com</span><span class="s2">.</span><span class="s1">service</span><span class="s2">.</span><span class="s1">TokenService</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">com</span><span class="s2">.</span><span class="s1">utils</span><span class="s2">.</span><span class="s1">CommonUtil</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">com</span><span class="s2">.</span><span class="s1">utils</span><span class="s2">.</span><span class="s1">PageUtils</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">com</span><span class="s2">.</span><span class="s1">utils</span><span class="s2">.</span><span class="s1">Query</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">org</span><span class="s2">.</span><span class="s1">springframework</span><span class="s2">.</span><span class="s1">stereotype</span><span class="s2">.</span><span class="s1">Service</span><span class="s2">;</span>

<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">util</span><span class="s2">.</span><span class="s1">Calendar</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">util</span><span class="s2">.</span><span class="s1">Date</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">util</span><span class="s2">.</span><span class="s1">List</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">util</span><span class="s2">.</span><span class="s1">Map</span><span class="s2">;</span>


<span class="s3">/**</span>
 <span class="s3">* token</span>
 <span class="s3">*/</span>
<span class="s1">@Service</span><span class="s2">(</span><span class="s4">&quot;tokenService&quot;</span><span class="s2">)</span>
<span class="s0">public class </span><span class="s1">TokenServiceImpl </span><span class="s0">extends </span><span class="s1">ServiceImpl</span><span class="s2">&lt;</span><span class="s1">TokenDao</span><span class="s2">, </span><span class="s1">TokenEntity</span><span class="s2">&gt; </span><span class="s0">implements </span><span class="s1">TokenService </span><span class="s2">{</span>

	<span class="s1">@Override</span>
	<span class="s0">public </span><span class="s1">PageUtils queryPage</span><span class="s2">(</span><span class="s1">Map</span><span class="s2">&lt;</span><span class="s1">String</span><span class="s2">, </span><span class="s1">Object</span><span class="s2">&gt; </span><span class="s1">params</span><span class="s2">) {</span>
		<span class="s1">Page</span><span class="s2">&lt;</span><span class="s1">TokenEntity</span><span class="s2">&gt; </span><span class="s1">page </span><span class="s2">= </span><span class="s0">this</span><span class="s2">.</span><span class="s1">selectPage</span><span class="s2">(</span>
                <span class="s0">new </span><span class="s1">Query</span><span class="s2">&lt;</span><span class="s1">TokenEntity</span><span class="s2">&gt;(</span><span class="s1">params</span><span class="s2">).</span><span class="s1">getPage</span><span class="s2">(),</span>
                <span class="s0">new </span><span class="s1">EntityWrapper</span><span class="s2">&lt;</span><span class="s1">TokenEntity</span><span class="s2">&gt;()</span>
        <span class="s2">);</span>
        <span class="s0">return new </span><span class="s1">PageUtils</span><span class="s2">(</span><span class="s1">page</span><span class="s2">);</span>
	<span class="s2">}</span>

	<span class="s1">@Override</span>
	<span class="s0">public </span><span class="s1">List</span><span class="s2">&lt;</span><span class="s1">TokenEntity</span><span class="s2">&gt; </span><span class="s1">selectListView</span><span class="s2">(</span><span class="s1">Wrapper</span><span class="s2">&lt;</span><span class="s1">TokenEntity</span><span class="s2">&gt; </span><span class="s1">wrapper</span><span class="s2">) {</span>
		<span class="s0">return </span><span class="s1">baseMapper</span><span class="s2">.</span><span class="s1">selectListView</span><span class="s2">(</span><span class="s1">wrapper</span><span class="s2">);</span>
	<span class="s2">}</span>

	<span class="s1">@Override</span>
	<span class="s0">public </span><span class="s1">PageUtils queryPage</span><span class="s2">(</span><span class="s1">Map</span><span class="s2">&lt;</span><span class="s1">String</span><span class="s2">, </span><span class="s1">Object</span><span class="s2">&gt; </span><span class="s1">params</span><span class="s2">,</span>
			<span class="s1">Wrapper</span><span class="s2">&lt;</span><span class="s1">TokenEntity</span><span class="s2">&gt; </span><span class="s1">wrapper</span><span class="s2">) {</span>
		 <span class="s1">Page</span><span class="s2">&lt;</span><span class="s1">TokenEntity</span><span class="s2">&gt; </span><span class="s1">page </span><span class="s2">=</span><span class="s0">new </span><span class="s1">Query</span><span class="s2">&lt;</span><span class="s1">TokenEntity</span><span class="s2">&gt;(</span><span class="s1">params</span><span class="s2">).</span><span class="s1">getPage</span><span class="s2">();</span>
	        <span class="s1">page</span><span class="s2">.</span><span class="s1">setRecords</span><span class="s2">(</span><span class="s1">baseMapper</span><span class="s2">.</span><span class="s1">selectListView</span><span class="s2">(</span><span class="s1">page</span><span class="s2">,</span><span class="s1">wrapper</span><span class="s2">));</span>
	    	<span class="s1">PageUtils pageUtil </span><span class="s2">= </span><span class="s0">new </span><span class="s1">PageUtils</span><span class="s2">(</span><span class="s1">page</span><span class="s2">);</span>
	    	<span class="s0">return </span><span class="s1">pageUtil</span><span class="s2">;</span>
	<span class="s2">}</span>

	<span class="s1">@Override</span>
	<span class="s0">public </span><span class="s1">String generateToken</span><span class="s2">(</span><span class="s1">Long userid</span><span class="s2">,</span><span class="s1">String username</span><span class="s2">, </span><span class="s1">String tableName</span><span class="s2">, </span><span class="s1">String role</span><span class="s2">) {</span>
		<span class="s1">TokenEntity tokenEntity </span><span class="s2">= </span><span class="s0">this</span><span class="s2">.</span><span class="s1">selectOne</span><span class="s2">(</span><span class="s0">new </span><span class="s1">EntityWrapper</span><span class="s2">&lt;</span><span class="s1">TokenEntity</span><span class="s2">&gt;().</span><span class="s1">eq</span><span class="s2">(</span><span class="s4">&quot;userid&quot;</span><span class="s2">, </span><span class="s1">userid</span><span class="s2">).</span><span class="s1">eq</span><span class="s2">(</span><span class="s4">&quot;role&quot;</span><span class="s2">, </span><span class="s1">role</span><span class="s2">));</span>
		<span class="s1">String token </span><span class="s2">= </span><span class="s1">CommonUtil</span><span class="s2">.</span><span class="s1">getRandomString</span><span class="s2">(</span><span class="s5">32</span><span class="s2">);</span>
		<span class="s1">Calendar cal </span><span class="s2">= </span><span class="s1">Calendar</span><span class="s2">.</span><span class="s1">getInstance</span><span class="s2">();   </span>
    	<span class="s1">cal</span><span class="s2">.</span><span class="s1">setTime</span><span class="s2">(</span><span class="s0">new </span><span class="s1">Date</span><span class="s2">());   </span>
    	<span class="s1">cal</span><span class="s2">.</span><span class="s1">add</span><span class="s2">(</span><span class="s1">Calendar</span><span class="s2">.</span><span class="s1">HOUR_OF_DAY</span><span class="s2">, </span><span class="s5">1</span><span class="s2">);</span>
		<span class="s0">if</span><span class="s2">(</span><span class="s1">tokenEntity</span><span class="s2">!=</span><span class="s0">null</span><span class="s2">) {</span>
			<span class="s1">tokenEntity</span><span class="s2">.</span><span class="s1">setToken</span><span class="s2">(</span><span class="s1">token</span><span class="s2">);</span>
			<span class="s1">tokenEntity</span><span class="s2">.</span><span class="s1">setExpiratedtime</span><span class="s2">(</span><span class="s1">cal</span><span class="s2">.</span><span class="s1">getTime</span><span class="s2">());</span>
			<span class="s0">this</span><span class="s2">.</span><span class="s1">updateById</span><span class="s2">(</span><span class="s1">tokenEntity</span><span class="s2">);</span>
		<span class="s2">} </span><span class="s0">else </span><span class="s2">{</span>
			<span class="s0">this</span><span class="s2">.</span><span class="s1">insert</span><span class="s2">(</span><span class="s0">new </span><span class="s1">TokenEntity</span><span class="s2">(</span><span class="s1">userid</span><span class="s2">,</span><span class="s1">username</span><span class="s2">, </span><span class="s1">tableName</span><span class="s2">, </span><span class="s1">role</span><span class="s2">, </span><span class="s1">token</span><span class="s2">, </span><span class="s1">cal</span><span class="s2">.</span><span class="s1">getTime</span><span class="s2">()));</span>
		<span class="s2">}</span>
		<span class="s0">return </span><span class="s1">token</span><span class="s2">;</span>
	<span class="s2">}</span>

	<span class="s1">@Override</span>
	<span class="s0">public </span><span class="s1">TokenEntity getTokenEntity</span><span class="s2">(</span><span class="s1">String token</span><span class="s2">) {</span>
		<span class="s1">TokenEntity tokenEntity </span><span class="s2">= </span><span class="s0">this</span><span class="s2">.</span><span class="s1">selectOne</span><span class="s2">(</span><span class="s0">new </span><span class="s1">EntityWrapper</span><span class="s2">&lt;</span><span class="s1">TokenEntity</span><span class="s2">&gt;().</span><span class="s1">eq</span><span class="s2">(</span><span class="s4">&quot;token&quot;</span><span class="s2">, </span><span class="s1">token</span><span class="s2">));</span>
		<span class="s0">if</span><span class="s2">(</span><span class="s1">tokenEntity </span><span class="s2">== </span><span class="s0">null </span><span class="s2">|| </span><span class="s1">tokenEntity</span><span class="s2">.</span><span class="s1">getExpiratedtime</span><span class="s2">().</span><span class="s1">getTime</span><span class="s2">()&lt;</span><span class="s0">new </span><span class="s1">Date</span><span class="s2">().</span><span class="s1">getTime</span><span class="s2">()) {</span>
			<span class="s0">return null</span><span class="s2">;</span>
		<span class="s2">}</span>
		<span class="s0">return </span><span class="s1">tokenEntity</span><span class="s2">;</span>
	<span class="s2">}</span>
<span class="s2">}</span>
</pre>
</body>
</html>